﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Citrix.XaAdminMobile.ViewModel;

namespace Citrix.XaAdminMobile.ViewCommon
{
    /// <summary>
    /// Interaction logic for AddServerView.xaml
    /// </summary>
    public partial class AddServerView : UserControl
    {
        /// <summary>
        /// View-model reference.
        /// </summary>
        private AddServerViewModel vm;

        /// <summary>
        /// Ctor
        /// </summary>
        public AddServerView()
        {
            InitializeComponent();

            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(AddServerView_IsVisibleChanged);
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(AddServerView_DataContextChanged);
        }

        /// <summary>
        /// Used to track a typed reference to our view-model.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddServerView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.vm = e.NewValue as AddServerViewModel;
        }

        /// <summary>
        /// Visbility changed handler. We hide the on screen keyboard when we lose visibility.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddServerView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (!(bool)e.NewValue)
            {
                Citrix.Cmp.ClientKeyboardController.HideKeyboard();
            }
        }

        /// <summary>
        /// Handle an enter key press for the server name entry. We simulate a Save button click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && this.vm != null)
            {
                this.vm.SaveCommand.Execute();
            }
        }
    }
}
